package com.dynamixsoftware.printershare;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ZoomButtonsController;
import android.widget.ZoomButtonsController.OnZoomListener;

public class ActivityPreview extends ActivityRoot {

	boolean lst_scroll = false;
	boolean lst_scroll_draw = false;
	private final Runnable mDismissOnScreenControlRunner = new Runnable() {
		public void run() {
			ActivityPreview.this.hideOnScreenControls();
		}
	};
	private GestureDetector mGestureDetector;
	private Handler mHandler = new Handler();
	ActivityPreview.PageImageView mPageView;
	private ZoomButtonsController mZoomButtonsController;

	private void hideOnScreenControls() {
		this.mZoomButtonsController.setVisible(false);
	}

	private void scheduleDismissOnScreenControls() {
		this.mHandler.removeCallbacks(this.mDismissOnScreenControlRunner);
		this.mHandler.postDelayed(this.mDismissOnScreenControlRunner, 2000L);
	}

	private void setupZoomButtonController(final View var1, final OnTouchListener var2) {
		this.mGestureDetector = new GestureDetector(this, new ActivityPreview.MyGestureListener(null));
		this.mZoomButtonsController = new ZoomButtonsController(var1) {
			public void setVisible(boolean var1) {
				if (var1 ^ this.isVisible()) {
					super.setVisible(var1);
				}

			}
		};
		this.mZoomButtonsController.setAutoDismissed(false);
		this.mZoomButtonsController.setZoomSpeed(100L);
		this.mZoomButtonsController.setOnZoomListener(new OnZoomListener() {
			public void onVisibilityChanged(boolean var1x) {
				if (var1x) {
					ActivityPreview.this.updateZoomButtonsEnabled();
				} else {
					var1.setOnTouchListener(var2);
				}
			}

			public void onZoom(boolean var1x) {
				if (var1x) {
					ActivityPreview.this.mPageView.zoomIn();
				} else {
					ActivityPreview.this.mPageView.zoomOut();
				}

				ActivityPreview.this.updateZoomButtonsEnabled();
			}
		});
	}

	private void showOnScreenControls() {
		this.updateZoomButtonsEnabled();
		this.mZoomButtonsController.setVisible(true);
	}

	private void updateZoomButtonsEnabled() {
		boolean var1 = true;
		float var2 = this.mPageView.getScale();
		ZoomButtonsController var3 = this.mZoomButtonsController;
		boolean var4;
		if (var2 < this.mPageView.mMaxZoom) {
			var4 = var1;
		} else {
			var4 = false;
		}

		var3.setZoomInEnabled(var4);
		ZoomButtonsController var5 = this.mZoomButtonsController;
		if (var2 <= 1.0F) {
			var1 = false;
		}

		var5.setZoomOutEnabled(var1);
	}

//	public boolean dispatchTouchEvent(MotionEvent var1) {
//	      boolean var2 = true;
//	      if(this.mZoomButtonsController.isVisible() && this.mZoomButtonsController.onTouch((View)null, var1)) {
//	         this.scheduleDismissOnScreenControls();
//	      }
//
//	      boolean var3;
//	      if(var1.getAction()) {
//	         var3 = var2;
//	      } else {
//	         var3 = false;
//	      }
//
//	      if((var3 & this.lst_scroll) != 0) {
//	         this.lst_scroll = false;
//	         if(this.lst_scroll_draw) {
//	            this.mPageView.pd.invalidateSelf();
//	         }
//
//	         this.lst_scroll_draw = false;
//	      }
//
//	      if(!super.dispatchTouchEvent(var1)) {
//	         var2 = this.mGestureDetector.onTouchEvent(var1);
//	      }
//
//	      return (boolean)var2;
//	   }


	public void onConfigurationChanged(Configuration var1) {
		super.onConfigurationChanged(var1);
		if (this.mPageView.pd != null) {
			this.mPageView.setPicture(this.mPageView.pd.getPicture());
		}

	}

	public void onCreate(Bundle var1) {
		super.onCreate(var1);
		this.getWindow().setFlags(1024, 1024);
		this.requestWindowFeature(1);
		this.mPageView = new ActivityPreview.PageImageView(this);
		this.setContentViewOrig(this.mPageView);
		OnTouchListener var3 = new OnTouchListener() {
			public boolean onTouch(View var1, MotionEvent var2) {
				ActivityPreview.this.scheduleDismissOnScreenControls();
				if (var2.getAction() == 0) {
					ActivityPreview.this.showOnScreenControls();
				}

				return false;
			}
		};
		this.setupZoomButtonController(this.mPageView, var3);
		this.mPageView.setOnTouchListener(var3);
		if (ActivityPrint.pp != null) {
			this.mPageView.setPicture(ActivityPrint.pp);
			ActivityPrint.pp = null;
		}

	}

	protected void onDestroy() {
		this.mPageView.setPicture((Picture) null);
		super.onDestroy();
	}

	@SuppressLint({ "MissingSuperCall" })
	public void onDetachedFromWindow() {
		if (this.mZoomButtonsController != null) {
			this.hideOnScreenControls();
		}

	}

	public boolean onKeyDown(int var1, KeyEvent var2) {
		if (this.mPageView.getVisibility() == 0 && var1 == 4) {
			if (this.mPageView.getScale() > 1.0F) {
				this.mPageView.zoomTo(1.0F);
			} else {
				this.finish();
			}

			return true;
		} else {
			return super.onKeyDown(var1, var2);
		}
	}

	class PageImageView extends ImageView {

		static final float SCALE_RATE = 1.25F;
		Matrix mBaseMatrix = new Matrix();
		private final Matrix mDisplayMatrix = new Matrix();
		private final float[] mMatrixValues = new float[9];
		float mMaxZoom;
		Matrix mSuppMatrix = new Matrix();
		private PictureDrawable pd;

		public PageImageView(Context var2) {
			super(var2);
			this.setScaleType(ScaleType.MATRIX);
			this.setBackgroundColor(-16777216);
		}

		private void getProperBaseMatrix(Picture var1, Matrix var2) {
			float var3 = (float) this.getWidth();
			float var4 = (float) this.getHeight();
			float var5 = (float) var1.getWidth();
			float var6 = (float) var1.getHeight();
			var2.reset();
			float var7 = Math.min(Math.min(var3 / var5, 2.0F), Math.min(var4 / var6, 2.0F));
			var2.postScale(var7, var7);
			var2.postTranslate((var3 - var5 * var7) / 2.0F, (var4 - var6 * var7) / 2.0F);
		}

		void center(boolean var1, boolean var2) {
			if (this.pd != null) {
				Matrix var3 = this.getImageViewMatrix();
				RectF var4 = new RectF(0.0F, 0.0F, (float) this.pd.getPicture().getWidth(), (float) this.pd
						.getPicture().getHeight());
				var3.mapRect(var4);
				float var6 = var4.height();
				float var7 = var4.width();
				float var8 = 0.0F;
				if (var2) {
					int var12 = this.getHeight();
					if (var6 < (float) var12) {
						var8 = ((float) var12 - var6) / 2.0F - var4.top;
					} else if (var4.top > 0.0F) {
						var8 = -var4.top;
					} else {
						float var14;
						int var13 = (var14 = var4.bottom - (float) var12) == 0.0F ? 0 : (var14 < 0.0F ? -1 : 1);
						var8 = 0.0F;
						if (var13 < 0) {
							var8 = (float) this.getHeight() - var4.bottom;
						}
					}
				}

				float var9 = 0.0F;
				if (var1) {
					int var10 = this.getWidth();
					if (var7 < (float) var10) {
						var9 = ((float) var10 - var7) / 2.0F - var4.left;
					} else if (var4.left > 0.0F) {
						var9 = -var4.left;
					} else {
						float var15;
						int var11 = (var15 = var4.right - (float) var10) == 0.0F ? 0 : (var15 < 0.0F ? -1 : 1);
						var9 = 0.0F;
						if (var11 < 0) {
							var9 = (float) var10 - var4.right;
						}
					}
				}

				this.postTranslate(var9, var8);
				this.setImageMatrix(this.getImageViewMatrix());
			}
		}

		Matrix getImageViewMatrix() {
			this.mDisplayMatrix.set(this.mBaseMatrix);
			this.mDisplayMatrix.postConcat(this.mSuppMatrix);
			return this.mDisplayMatrix;
		}

		float getScale() {
			return this.getScale(this.mSuppMatrix);
		}

		float getScale(Matrix var1) {
			return this.getValue(var1, 0);
		}

		float getValue(Matrix var1, int var2) {
			var1.getValues(this.mMatrixValues);
			return this.mMatrixValues[var2];
		}

		float maxZoom() {
			if (this.pd == null) {
				return 1.0F;
			} else {
				float var1 = (float) this.getWidth();
				float var2 = (float) this.getHeight();
				return 2.0F * Math.max((float) this.pd.getPicture().getWidth() / var1, (float) this.pd.getPicture()
						.getHeight() / var2);
			}
		}

		protected void onLayout(boolean var1, int var2, int var3, int var4, int var5) {
			super.onLayout(var1, var2, var3, var4, var5);
			if (this.pd != null) {
				this.getProperBaseMatrix(this.pd.getPicture(), this.mBaseMatrix);
				this.setImageMatrix(this.getImageViewMatrix());
				this.mMaxZoom = this.maxZoom();
			}

		}

		void panBy(float var1, float var2) {
			this.postTranslate(var1, var2);
			this.setImageMatrix(this.getImageViewMatrix());
		}

		void postTranslate(float var1, float var2) {
			this.mSuppMatrix.postTranslate(var1, var2);
		}

		protected void postTranslateCenter(float var1, float var2) {
			this.postTranslate(var1, var2);
			this.center(true, true);
		}

		public void setPicture(Picture var1) {
			if (this.pd != null) {
				this.pd.setPicture((Picture) null);
			}

			this.pd = null;
			if (var1 != null) {
				this.pd = new PictureDrawable((Picture) null) {
					public void draw(Canvas var1) {
						Picture var2 = this.getPicture();
						if (var2 != null) {
							Rect var3 = this.getBounds();
							var1.save();
							var1.clipRect(var3);
							var1.translate((float) var3.left, (float) var3.top);
							if (var2 instanceof ActivityPrintDocuments.DocPicture) {
								ActivityPrintDocuments.DocPicture var6 = (ActivityPrintDocuments.DocPicture) var2;
								boolean var7;
								if (!ActivityPreview.this.lst_scroll) {
									var7 = true;
								} else {
									var7 = false;
								}

								var6.draw(var1, var7);
							} else {
								var2.draw(var1);
							}

							var1.restore();
							if (ActivityPreview.this.lst_scroll) {
								ActivityPreview.this.lst_scroll_draw = true;
							}
						}

					}
				};
				this.pd.setPicture(var1);
				this.getProperBaseMatrix(var1, this.mBaseMatrix);
			} else {
				this.mBaseMatrix.reset();
			}

			this.mSuppMatrix.reset();
			this.setImageMatrix(this.getImageViewMatrix());
			this.mMaxZoom = this.maxZoom();
			this.setImageDrawable(this.pd);
		}

		void zoomIn() {
			this.zoomIn(1.25F);
		}

		void zoomIn(float var1) {
			if (this.getScale() < this.mMaxZoom && this.pd != null) {
				float var2 = (float) this.getWidth() / 2.0F;
				float var3 = (float) this.getHeight() / 2.0F;
				this.mSuppMatrix.postScale(var1, var1, var2, var3);
				this.setImageMatrix(this.getImageViewMatrix());
			}
		}

		void zoomOut() {
			this.zoomOut(1.25F);
		}

		void zoomOut(float var1) {
			if (this.pd != null) {
				float var2 = (float) this.getWidth() / 2.0F;
				float var3 = (float) this.getHeight() / 2.0F;
				Matrix var4 = new Matrix(this.mSuppMatrix);
				var4.postScale(1.0F / var1, 1.0F / var1, var2, var3);
				if (this.getScale(var4) < 1.0F) {
					this.mSuppMatrix.setScale(1.0F, 1.0F, var2, var3);
				} else {
					this.mSuppMatrix.postScale(1.0F / var1, 1.0F / var1, var2, var3);
				}

				this.setImageMatrix(this.getImageViewMatrix());
				this.center(true, true);
			}
		}

		void zoomTo(float var1) {
			this.zoomTo(var1, (float) this.getWidth() / 2.0F, (float) this.getHeight() / 2.0F);
		}

		void zoomTo(float var1, float var2, float var3) {
			if (var1 > this.mMaxZoom) {
				var1 = this.mMaxZoom;
			}

			float var4 = var1 / this.getScale();
			this.mSuppMatrix.postScale(var4, var4, var2, var3);
			this.setImageMatrix(this.getImageViewMatrix());
			this.center(true, true);
		}
	}

	private class MyGestureListener extends SimpleOnGestureListener {

		private MyGestureListener() {
		}

		// $FF: synthetic method
		MyGestureListener(Object var2) {
			this();
		}

		public boolean onScroll(MotionEvent var1, MotionEvent var2, float var3, float var4) {
			if (ActivityPreview.this.mPageView.getScale() > 1.0F) {
				ActivityPreview.this.lst_scroll = true;
				ActivityPreview.this.mPageView.postTranslateCenter(-var3, -var4);
			}

			return true;
		}
	}
}
